﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AMS.LINQ;

namespace AMS.Business
{
    public class AnalysisMethodGroupClass
    {
        String analysisMethodGroup;
        int analysisMethodGroupID = 0;

        bool isDirty = false;
        bool isNew = true;

        //AnalysisMethodGroupTableDataContext amgContext = new AnalysisMethodGroupTableDataContext();
        //AnalysisMethodTableDataContext amContext = new AnalysisMethodTableDataContext();

        #region Properties
        public String AnalysisMethodGroup
        {
            get 
            {
                return analysisMethodGroup;
            }
            set
            {
                analysisMethodGroup = value;
                isDirty = true;
            }
        }

        public int AnalysisMethodGroupID
        {
            get
            {
                return analysisMethodGroupID;
            }
        }
        #endregion

        #region Methods
        public AnalysisMethodGroupClass()
        {
            isNew = true;
            isDirty = false;
        }

        public void Save()
        {
            if (isNew)
            {
                AnalysisMethodGroup newAnalysisMethodGroup = new AnalysisMethodGroup();
                newAnalysisMethodGroup.AnalysisMethodGroupName = analysisMethodGroup;

                Common.AnalyseMethodGroup().AnalysisMethodGroups.InsertOnSubmit(newAnalysisMethodGroup);
                Common.AnalyseMethodGroup().SubmitChanges();

                // Get the ID
                analysisMethodGroupID = newAnalysisMethodGroup.AnalysisMethodGroupID;
            }
            else if (isDirty)
            {
                var dirtyAnalysisMethodGroup = from dirty in Common.AnalyseMethodGroup().AnalysisMethodGroups
                                               where dirty.AnalysisMethodGroupID == analysisMethodGroupID
                                               select dirty;

                dirtyAnalysisMethodGroup.SingleOrDefault().AnalysisMethodGroupName = analysisMethodGroup;
                Common.AnalyseMethodGroup().SubmitChanges();
            }

            isDirty = false;
            isNew = false;
        }

        public bool Delete()
        {
            var canBeDeleted = from cbd in Common.AnalyseMethod().AnalysisMethods
                                   where cbd.AnalysisMethodGroupID_FK == analysisMethodGroupID
                                   select cbd;

            if ((Int32)canBeDeleted.Count() != 0)
            {
                return false;
            }

            var toBeDeleted = from tbd in Common.AnalyseMethodGroup().AnalysisMethodGroups
                              where tbd.AnalysisMethodGroupID == analysisMethodGroupID
                              select tbd;

            Common.AnalyseMethodGroup().AnalysisMethodGroups.DeleteOnSubmit(toBeDeleted.SingleOrDefault());
            Common.AnalyseMethodGroup().SubmitChanges();

            isNew = false;
            isDirty = false;

            return true;
        }

        public void Load(int Id)
        {
            var loaded = from load in Common.AnalyseMethodGroup().AnalysisMethodGroups
                         where load.AnalysisMethodGroupID == Id
                         select load;

            analysisMethodGroupID = Id;
            analysisMethodGroup = loaded.SingleOrDefault().AnalysisMethodGroupName;

            isNew = true;
            isDirty = false;
        }
        #endregion
    }
}
